'''
Created on 2018年10月24日

'''
from django.conf import settings
from django.core.files.storage import FileSystemStorage


class Storage(FileSystemStorage):

    def __init__(self, location=settings.MEDIA_ROOT, base_url=settings.MEDIA_URL):
        super(Storage,self).__init__(location, base_url)
    
    def _save(self, name, content):
        #name为上传文件名称
        import os, time, random
        # 文件扩展名
        ext = os.path.splitext(name)[1]
        # 文件目录
        d = os.path.dirname(name)
        # 定义文件名，年月日时分秒随机数
        fn = time.strftime('%Y%m%d%H%M%S')
        fn = fn + '%d' % random.randint(1,1000)
        # 重写合成文件名
        name = os.path.join(d, fn + ext)

        return super(Storage,self)._save(name, content)
        